﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAO;
using DTO;
namespace BUS
{
   public class SanPhamBUS
    {
       public const int trungTen = 0;
       public const int thanhCong = 1;
       public const int thatBai = 2;
        public static int ThemSanPham(SanPhamDTO sanPham)
        {
           
            if (LayDSSanPham() != null)
            {
                List<SanPhamDTO> listSP = LayDSSanPham(-1, sanPham.TenSanPham);
                if (listSP.Count > 0 )
                    return trungTen;
            }
            if (!SanPhamDAO.ThemSanPham(sanPham))
                return thatBai;
            return thanhCong;
        }

        public static int SuaThongTinSanPham(SanPhamDTO sanPham)
        {
            if (LayDSSanPham() != null)
            {
                List<SanPhamDTO> listSP =LayDSSanPham(-1, sanPham.TenSanPham);
                if (listSP.Count > 0 && listSP[0].MaSanPham != sanPham.MaSanPham)
                    return trungTen; 
            }
            if (!SanPhamDAO.SuaThongTinSanPham(sanPham))
                return thatBai;
            return thanhCong;
        }
       public static int SuaSoLuongSanPham(SanPhamDTO sanPham)
       {
           if (!SanPhamDAO.SuaThongTinSanPham(sanPham))
                return thatBai;
            return thanhCong;
       }
        public static bool XoaSanPham(int maSanPham)
        {
            if (SanPhamDAO.XoaSanPham(maSanPham))
            {
                //Xoa cac chi tiet thue phong lien quan
                List<ChiTietPhong_SanPhamDTO> dsCT = ChiTietPhong_SanPhamBUS.LayDSChiTietPhong_SanPham(-1, -1,maSanPham);
                if (dsCT != null && dsCT.Count > 0)
                {
                    foreach (var item in dsCT)
                        if (!ChiTietPhong_SanPhamBUS.XoaChiTietPhong_SanPham(item.MaCT))
                            return false;
                }
                return true;
            }
            return false;
        }
        public static List<SanPhamDTO> LayDSSanPham(int maSanPham = -1, String tenSanPham = null)
        {
            return SanPhamDAO.LayDSSanPham(maSanPham, tenSanPham);
        }
    }
}
